<?php

namespace app\common\model;

use think\Model;

/**
 * 数据层模型
 */
class Fujineng extends Model {

    /**
     * 查找
     */
    public function getList($condition=[],$field='*',$page=0,$order='ds_fuwu_personal_jineng.id desc')
    {
        if ($page)
        {
            $res = db('fuwu_personal_jineng')->where($condition)->field($field)
                ->join('ds_goodsclass g','g.gc_id = ds_fuwu_personal_jineng.cateone','left')
                ->join('ds_goodsclass g2','g2.gc_id = ds_fuwu_personal_jineng.catetwo','left')
                ->join('ds_goodsclass g3','g3.gc_id = ds_fuwu_personal_jineng.catrthree','left')
                ->join('ds_brand b','b.brand_id = ds_fuwu_personal_jineng.brandid','left')
                ->join('ds_fuwu_personal p','p.id = ds_fuwu_personal_jineng.fuwuid','left')
//                ->join('ds_fuwu_grade g','g.id = ds_fuwu_personal.gradeid','left')
                ->order($order)->paginate($page,false,['query'=>request()->param()]);
            $this->page_info=$res;
            return $res->items();
        }else{
            return db('fuwu_personal')->where($condition)->field($field)->order($order)->select();
        }
    }

    /**
     * 查找
     */
    public function getSearchList($condition=[],$field='*',$page=0,$order='ds_fuwu_personal_jineng.id desc')
    {
        if ($page)
        {
            $res = db('fuwu_personal_jineng')->where($condition)->field($field)
                ->join('ds_goodsclass g','g.gc_id = ds_fuwu_personal_jineng.cateone','left')
                ->join('ds_goodsclass g2','g2.gc_id = ds_fuwu_personal_jineng.catetwo','left')
                ->join('ds_goodsclass g3','g3.gc_id = ds_fuwu_personal_jineng.catrthree','left')
                ->join('ds_brand b','b.brand_id = ds_fuwu_personal_jineng.brandid','left')
                ->join('ds_fuwu_personal p','p.id = ds_fuwu_personal_jineng.fuwuid','left')
//                ->join('ds_fuwu_grade g','g.id = ds_fuwu_personal.gradeid','left')
                ->order($order)->paginate($page,false,['query'=>request()->param()]);
            $this->page_info=$res;
            return $res->items();
        }else{
            return db('fuwu_personal')->where($condition)->field($field)->order($order)->select();
        }
    }

    /**
     * 添加
     */
    public function  add($data)
    {
        return db('fuwu_personal_jineng')->insertGetId($data);
    }

    /**
     * 修改
     */
    public function edit($condition=[],$data)
    {
        return db('fuwu_personal_jineng')->where($condition)->update($data);
    }

    /**
     * 删除
     */
    public function del($condition=[])
    {
        return db('fuwu_personal_jineng')->where($condition)->delete();
    }

    /**
     * 获取单个详情
     */
    public function info($condition=[],$field='*')
    {
        return db('fuwu_personal_jineng')->where($condition)->field($field)->find();
    }

    /**
     * 获取邀请人id和类型
     */
    public function getMember($code)
    {
        $returnArr = [];
        $res = $this->info(['code'=>$code]);
        if ($res){
            /**
             * 服务人员
             */
            $returnArr['prev'] = $res['id'];
            $returnArr['prevtype'] = 4;
        }
        $membermodel = model('member');
        $res2 = $membermodel->infoMember(['invite_code'=>$code]);
        if ($res2){
            /**
             * 运营商或者客户
             */
            $returnArr['prev'] = $res2['member_id'];
            if ($res2['invite_store_id']) {
                $returnArr['prevtype'] = 2;#运营商
            }else{
                $returnArr['prevtype'] = 3;#客户
            }
        }
        if (empty($res) && empty($res2)){
            $returnArr['prev'] = 0;
            $returnArr['prevtype'] = 1;
        }
        return $returnArr;
    }

    /**
     * 添加成功,修改积分
     */
    public function updateYue($previd, $prevtype)
    {
        $bool = true;
        if ($prevtype == 3){
            $value = db('config')->where(['code'=>'purchase_goods'])->find();
            #普通客户邀请服务人员,修改普通用户积分
            $model = model('member');
            $money = $model->getMemberInfo(['member_id'=>$previd],'member_points');
            $vmoney = $money['member_points']+intval($value['value']);
            $bool = $model->editMember(['member_id'=>$previd],['member_points'=>$vmoney]);
        }
        if ($prevtype==4){
            #服务人员邀请服务人员,修改服务人员余额
            $value = db('config')->where(['code'=>'service_invitation'])->find();
            $money = $this->info(['id'=>$previd],'money');
            $vmoney = $money['money']+intval($value['value']);
            $bool = $this->edit(['id'=>$previd],['money'=>$vmoney]);
        }
        if ($bool){
            return true;
        }else{
            return false;
        }
    }
}
?>
